package net.easyjava.web.controller;

import net.easyjava.base.bean.ArticleBean;
import net.easyjava.base.bean.cache.ArticleCacheBean;
import net.easyjava.base.constants.AlbumConstants;
import net.easyjava.base.constants.CommonConstants;
import net.easyjava.base.manager.ArticleManager;
import net.easyjava.base.utils.qiniu.QiniuUploadUtils;
import net.easyjava.tools.http.HttpTools;
import net.easyjava.tools.lang.StringTools;
import net.easyjava.tools.model.ModelConstants;
import net.easyjava.web.constants.WebConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wxnacy on 16/1/1.
 */
@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private ArticleManager articleManager;
    //放弃ftp
//    @Value("${article.address}")
//    private String ARTICLE_ADDRESS;

    /**
     * 文章列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/list/{albumId}.html")
    @ResponseBody
    public ModelAndView list(
            @PathVariable int albumId,
            @RequestParam(required = false,defaultValue = CommonConstants.PAGE_NUM+"") int pageNum,
            @RequestParam(required = false,defaultValue = CommonConstants.PAGE_SIZE+"") int pageSize,
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            ArticleBean articleBean = new ArticleBean();
            articleBean.setAlbumId(albumId);

            params.put("page",articleManager.findList(articleBean,pageNum, pageSize));
            params.put("albumId",albumId);
            return new ModelAndView("web/article/list.html",params);
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }


    /**
     * 文章详情
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/view/{id}.html")
    @ResponseBody
    public ModelAndView view(
            @PathVariable String id,
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            ArticleCacheBean articleCacheBean = articleManager.findCache(id);
            params.put("article", articleCacheBean);
            params.put("content", articleManager.getArticleContent(articleCacheBean.getPath()));
            params.put("id",id);
            return new ModelAndView("web/article/view.html",params);
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }
}
